<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/15/015
 * Time: 9:55
 */

namespace app\api\controller;


use think\Controller;
use think\Db;

class Notice extends Controller
{

    //获取AccessToken
    public function getAccessToken($config)
    {
        $appId = $config['xcx_appid'];
        $appSecret = $config['xcx_appsecret'];
        $r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret"); //返回的是字符串，需要用json_decode转换成数组
        $data = json_decode($r,true);
        return $data['access_token'];
    }


    //招商签约提醒
    public function signMessage($data,$remark){
        $appid = "wx2cd7120426ce7ee6";
        $config = Db::name("config")->where(['xcx_appid' => $appid])->find();
        $token = $this->getAccessToken($config);

        //查询项目标题
        $title = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("title");

        //查询责任单位
        $duty_unit = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("duty_unit");


        //查询项目负责人
        $duty_person = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("duty_person");
        $duty_person = explode(";",$duty_person);


        $thing9 = "(招商签约库)".$title;
        //查询用户
        foreach ($duty_person as $item){
            $fans_id = 0;
            $fans_id = Db::name("project_account")->where(['name'=>$item])->value("fans_id");

            if(!empty($fans_id)){
                //用户openid
                $xcx_openid = Db::name("admin_fans")->where(['id'=>$fans_id])->value("xcx_openid");


                $post = [];
                $post['touser'] = $xcx_openid;
                $post['page'] = 'pages/message/message';
                $post['template_id'] = 'Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0';
                $post['data'] = [
                    'thing2'=>['value'=>$data['title'],'color'=>'#173177'],
                    'thing3'=>['value'=>$remark,'color'=>'#173177'],
                    'date8'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                    'thing9'=>['value'=>$this->msubstr($thing9,0, 14),'color'=>'#173177'],
                ];

                /*$post = [];
                $post['touser'] = $xcx_openid;
                $post['page'] = 'pages/message/message';
                $post['template_id'] = 'ceT-0KL7fkj6GuEwJvcvhfrqqXjl-W67qq2jclgUfF0';
                $post['data'] = [
                    'thing1'=>['value'=>$title,'color'=>'#173177'],
                    'thing4'=>['value'=>$project_user,'color'=>'#173177'],
                    'time5'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                    'thing6'=>['value'=>$data['content'],'color'=>'#173177'],
                ];*/
                $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
                $re = $this->curl_url($url,$post);
            }
        }
    }


    //重点项目逾期提醒
    public function copyMessage($project_user,$data)
    {
        $appid = "wx2cd7120426ce7ee6";
        $config = Db::name("config")->where(['xcx_appid' => $appid])->find();
        $token = $this->getAccessToken($config);
        //查询项目标题
        $title = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("title");

//        //查询责任单位
//        $duty_unit = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("duty_unit");

        //查询项目负责人
        $duty_person = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("duty_person");
        $duty_person = explode(";",$duty_person);


        //查询项目对应的库名
        $library = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("library");
        $libraryArray = [1=>"招商签约库",2=>"项目预备库",3=>"项目开工库",4=>"市重点项目库",5=>"入统库",6=>"双千"];

        $thing9 = "(".$libraryArray[$library].")".$this->msubstr($title,0, 8);
        //查询用户
        foreach ($duty_person as $item){
            $fans_id = 0;
            $fans_id = Db::name("project_account")->where(['name'=>$item])->value("fans_id");

            if(!empty($fans_id)){
                //用户openid
                $xcx_openid = Db::name("admin_fans")->where(['id'=>$fans_id])->value("xcx_openid");
                $post = [];
                $post['touser'] = $xcx_openid;
                $post['page'] = 'pages/message/message';
                $post['template_id'] = 'Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0';
                $post['data'] = [
                    'thing2'=>['value'=>"逾期提醒",'color'=>'#173177'],
                    'thing3'=>['value'=>"您的项目已经逾期",'color'=>'#173177'],
                    'date8'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                    'thing9'=>['value'=>$thing9,'color'=>'#173177'],
                ];
                $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
                $re = $this->curl_url($url,$post);
            }
        }


        //查询该项目的关联用户
        $account = Db::name("project_account_pro")->where(['project_id'=>$data['pro_id']])->field("id,account_id")->select();
        //查询用户
        foreach ($account as $item){
            $fans_id = 0;
            $fans_id = Db::name("project_account")->where(['id'=>$item['account_id']])->value("fans_id");
            if(!empty($fans_id)){
                //用户openid
                $xcx_openid = Db::name("admin_fans")->where(['id'=>$fans_id])->value("xcx_openid");
                $post = [];
                $post['touser'] = $xcx_openid;
                $post['page'] = 'pages/message/message';
                $post['template_id'] = 'Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0';
                $post['data'] = [
                    'thing2'=>['value'=>"逾期提醒",'color'=>'#173177'],
                    'thing3'=>['value'=>"您的项目已经逾期",'color'=>'#173177'],
                    'date8'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                    'thing9'=>['value'=>$thing9,'color'=>'#173177'],
                ];
                $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
                $re = $this->curl_url($url,$post);
            }
        }



    }

    //小程序端
    public function sendMessage($project_user,$data,$fans_id=0)
    {
        $appid = "wx2cd7120426ce7ee6";
        $config = Db::name("config")->where(['xcx_appid' => $appid])->find();
        $token = $this->getAccessToken($config);
        //查询项目标题
        $title = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("title");
        //点评人
        $name = Db::name("project_account")->where(['id'=>$project_user['id']])->value("name");


        //查询项目对应的库名
        $library = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("library");
        $libraryArray = [1=>"招商签约库",2=>"项目预备库",3=>"项目开工库",4=>"市重点项目库",5=>"入统库",6=>"双千"];


        $thing9 = "(".$libraryArray[$library].")".$this->msubstr($title,0, 8);
        if(empty($fans_id)){
            //查询项目负责人
            $duty_person = Db::name("project_pro")->where(['id'=>$data['pro_id']])->value("duty_person");
            $duty_person = explode(";",$duty_person);

            //查询用户
            foreach ($duty_person as $item){
                $fans_id = 0;
                $fans_id = Db::name("project_account")->where(['name'=>$item])->value("fans_id");
                if(!empty($fans_id)){
                    //用户openid
                    $xcx_openid = Db::name("admin_fans")->where(['id'=>$fans_id])->value("xcx_openid");
                    $post = [];
                    $post['touser'] = $xcx_openid;
                    $post['page'] = 'pages/message/message';
                    $post['template_id'] = 'Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0';
                    $post['data'] = [
                        'thing2'=>['value'=>"点评消息",'color'=>'#173177'],
                        'thing3'=>['value'=>"您的项目已被".$name."点评",'color'=>'#173177'],
                        'date8'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                        'thing9'=>['value'=>$thing9,'color'=>'#173177'],
                    ];
                    $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
                    $re = $this->curl_url($url,$post);
                }
            }

            //查询该项目的关联用户
            $account = Db::name("project_account_pro")->where(['project_id'=>$data['pro_id']])->field("id,account_id")->select();
            //查询用户
            foreach ($account as $item){
                $fans_id = 0;
                $fans_id = Db::name("project_account")->where(['id'=>$item['account_id']])->value("fans_id");
                if(!empty($fans_id)){
                    //用户openid
                    $xcx_openid = Db::name("admin_fans")->where(['id'=>$fans_id])->value("xcx_openid");
                    $post = [];
                    $post['touser'] = $xcx_openid;
                    $post['page'] = 'pages/message/message';
                    $post['template_id'] = 'Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0';
                    $post['data'] = [
                        'thing2'=>['value'=>"点评消息",'color'=>'#173177'],
                        'thing3'=>['value'=>"您的项目已被".$name."点评",'color'=>'#173177'],
                        'date8'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                        'thing9'=>['value'=>$thing9,'color'=>'#173177'],
                    ];
                    $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
                    $re = $this->curl_url($url,$post);
                }
            }



        }else{
            //用户openid
            $xcx_openid = Db::name("admin_fans")->where(['id'=>$fans_id])->value("xcx_openid");
            /*$post = [];
            $post['touser'] = $xcx_openid;
            $post['page'] = 'pages/message/message';
            $post['template_id'] = '1xfah8tCj6m7090cMuZTfhzOHiJu_hNRydigyvWYEbo';
            $post['data'] = [
                'thing1'=>['value'=>$data['content'],'color'=>'#173177'],
                'time2'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177']
            ];*/

            $post = [];
            $post['touser'] = $xcx_openid;
            $post['page'] = 'pages/message/message';
            $post['template_id'] = 'Nfr1gM-tFOcMvGWiisr49WZn6PiCt2U3QPBSocBFaO0';
            $post['data'] = [
                'thing2'=>['value'=>"回复消息",'color'=>'#173177'],
                'thing3'=>['value'=>"您的项目已被".$name."回复",'color'=>'#173177'],
                'date8'=>['value'=>date("Y-m-d H:i:s",time()),'color'=>'#173177'],
                'thing9'=>['value'=>$thing9,'color'=>'#173177'],
            ];


            $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
//        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$token;
            $re = $this->curl_url($url,$post);


        }
    }


    function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
    {
        if (function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length, $charset);
        elseif (function_exists('iconv_substr')) {
            $slice = iconv_substr($str, $start, $length, $charset);
            if (false === $slice) {
                $slice = '';
            }
        } else {
            $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("", array_slice($match[0], $start, $length));
        }
        //字数不满不添加...
        $count = mb_strlen($str, 'utf-8');
        if ($count > $length) {
            return $suffix ? $slice . '...' : $slice;
        } else {
            return $slice;
        }

    }


    private function curl_url($url, $json)
    {
        $body = json_encode($json);
        $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;

    }

}